package tmp;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

/**
 * Created by dycong on 2017/3/6.
 */
public class testBigDecimal {

    public static void main(String[] args) {

        System.out.println(new BigDecimal(10.40000000000).round(new MathContext(2, RoundingMode.UP)));     //11
        System.out.println(new BigDecimal(-10.4000000000).round(new MathContext(2, RoundingMode.UP)));     //-11
        System.out.println(new BigDecimal(10.50000000000).round(new MathContext(2, RoundingMode.UP)));     //11
        System.out.println(new BigDecimal(-10.50000000000).round(new MathContext(2, RoundingMode.UP)));     //-11
        System.out.println(new BigDecimal(10.60000000000).round(new MathContext(2, RoundingMode.UP)));     //11
        System.out.println(new BigDecimal(-10.60000000000).round(new MathContext(2, RoundingMode.UP)));     //-11

        System.out.println(new BigDecimal(10.40000000000).round(new MathContext(2, RoundingMode.DOWN)));     //-10
        System.out.println(new BigDecimal(-10.40000000000).round(new MathContext(2, RoundingMode.DOWN)));     //-10
        System.out.println(new BigDecimal(10.50000000000).round(new MathContext(2, RoundingMode.DOWN)));     //10
        System.out.println(new BigDecimal(-10.50000000000).round(new MathContext(2, RoundingMode.DOWN)));     //-10
        System.out.println(new BigDecimal(10.60000000000).round(new MathContext(2, RoundingMode.DOWN)));     //10
        System.out.println(new BigDecimal(-10.60000000000).round(new MathContext(2, RoundingMode.DOWN)));     //-10

        System.out.println(new BigDecimal(10.40000100000).round(new MathContext(2, RoundingMode.HALF_UP)));     //10
        System.out.println(new BigDecimal(-10.40001000000).round(new MathContext(2, RoundingMode.HALF_UP)));     //-10
        System.out.println(new BigDecimal(10.50000100000).round(new MathContext(2, RoundingMode.HALF_UP)));     //11
        System.out.println(new BigDecimal(-10.50001000000).round(new MathContext(2, RoundingMode.HALF_UP)));     //-11
        System.out.println(new BigDecimal(10.60000100000).round(new MathContext(2, RoundingMode.HALF_UP)));     //11
        System.out.println(new BigDecimal(-10.60001000000).round(new MathContext(2, RoundingMode.HALF_UP)));     //-11

        System.out.println(new BigDecimal(10.40000100000).round(new MathContext(2, RoundingMode.HALF_DOWN)));//10
        System.out.println(new BigDecimal(-10.40000000000).round(new MathContext(2, RoundingMode.HALF_DOWN)));//-10
        System.out.println(new BigDecimal(10.50000000000).round(new MathContext(2, RoundingMode.HALF_DOWN)));//10
        System.out.println(new BigDecimal(-10.50000000000).round(new MathContext(2, RoundingMode.HALF_DOWN)));//-10
/*注意此处，只要舍入位后边有值(不管第几位)，也就是说只要大于5一点点，就进位，进位*/
        System.out.println(new BigDecimal(10.50100000000).round(new MathContext(2, RoundingMode.HALF_DOWN)));//11
        System.out.println(new BigDecimal(-10.50100000000).round(new MathContext(2, RoundingMode.HALF_DOWN)));//-11
        System.out.println(new BigDecimal(10.60000100000).round(new MathContext(2, RoundingMode.HALF_DOWN)));//11
        System.out.println(new BigDecimal(-10.60001000000).round(new MathContext(2, RoundingMode.HALF_DOWN)));//-11

        System.out.println(new BigDecimal(10.400001).round(new MathContext(2, RoundingMode.HALF_EVEN)));       //10
        System.out.println(new BigDecimal(19.500000).round(new MathContext(2, RoundingMode.HALF_EVEN)));       //20 五后为零看奇偶，五前为奇应进
        System.out.println(new BigDecimal(19.500100).round(new MathContext(2, RoundingMode.HALF_EVEN)));       //20
        System.out.println(new BigDecimal(10.50000).round(new MathContext(2, RoundingMode.HALF_EVEN)));        //10 五后为零看奇偶，五前为偶应舍去
        System.out.println(new BigDecimal(10.50100).round(new MathContext(2, RoundingMode.HALF_EVEN)));        //11 五后非零就进一
        /*负数时，采用了类似Math.round()的对称舍入方式：大于5就向绝对值大的方向舍入，五后非零就向绝对值大的方向进一*/
        System.out.println(new BigDecimal(-10.400001).round(new MathContext(2, RoundingMode.HALF_EVEN)));       //-10
        System.out.println(new BigDecimal(-19.500000).round(new MathContext(2, RoundingMode.HALF_EVEN)));       //-20
        System.out.println(new BigDecimal(-19.500100).round(new MathContext(2, RoundingMode.HALF_EVEN)));       //-20
        System.out.println(new BigDecimal(-10.50000).round(new MathContext(2, RoundingMode.HALF_EVEN)));       //-10
        System.out.println(new BigDecimal(-10.50100).round(new MathContext(2, RoundingMode.HALF_EVEN)));       //-11
    }
}
